/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.threading;

import domain.DomainController;
import domain.net.HTMLParser;
import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.swing.text.html.HTML.Attribute;
import javax.swing.text.html.HTML.Tag;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import persistence.net.Client;
import persistence.net.FileValuePair;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class UploadImage extends Thread
{
    private File file;

    public UploadImage(File file)
    {
        this.file = file;
    }

    @Override
    public void run()
    {
        try
        {
            Client client = new Client();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("uploadtype", "image"));
            nameValuePairs.add(new BasicNameValuePair("submit", ""));

            FileValuePair fvp = new FileValuePair("image", file);

            client.doPostUploadFile("http://lulzimg.com/upload.php", nameValuePairs, fvp);

            String content = client.getResponseContent();

            //<input type="text" size="50" id="imagecode1" onclick="SelectAll('imagecode1');" value="http://lulzimg.com/i23/e151a6.jpg">
            List<String> results = HTMLParser.extractValueFromTag(new StringReader(content),
                                                                    Tag.INPUT,
                                                                    Attribute.ID,
                                                                    "imagecode1",
                                                                    Attribute.VALUE);
            String imageSrc = results.get(0);

            DomainController.getInstance().getLogger().log(Level.INFO, "Image uploaded: \"{0}\".", imageSrc);

            DomainController.getInstance().getUi().insertTagInPostBody("[img]" + imageSrc + "[/img]");
        }
        catch(Exception e)
        {
            DomainController.getInstance().getLogger().log(Level.SEVERE, "Error while uploading your image. {0}", e.getMessage());
        }
        finally
        {
            DomainController.getInstance().getUi().setButtonsEnabled(true);
            DomainController.getInstance().getUi().setStatusIndeterminate(false, "Done");
        }
    }
}